<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <script src="js/swiper-4.2.6.min.js" type="text/javascript" charset="utf-8"></script>
    <link rel="stylesheet" type="text/css" href="css/swiper-4.2.6.min.css"/>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="js/public.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/font.js"></script>
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/ui.css">
    <title></title>
    <!-- 确认订单 -->
    <style>
    </style>
</head>
<body>
<div class="scroll-content2 confirm-order" style="overflow:auto;background: #F5F5F5;">
    <div class="top-public">
        <span class="public-center">确认订单</span>
        <img src="images/left_icon.png" alt="" class="public-left">
    </div>
    <!-- <div class="address-box">
        <div class="address-one">
            <div class="address-left">
                <img src="images/order1.png" alt="">
            </div>
            <a href="invoiceAddress.html">
                <div class="address-center">
                    <div>购买人/单位：<span>杨洋</span></div>
                    <div>手机号：<span>156****0711</span></div>
                    <div>收货地址：<span>上海上海市普陀区 汇泉大厦9楼12室</span></div>
                    <img src="images/confirm-right.png" alt="" class="right">
                </div>
            </a>
        </div>
    </div> -->
    <div class="company-name" style="margin-top:0.2rem;">
        <img src="images/confirm-logo.png" alt="">
        <span>邦伲德（上海）网络科技有限公司</span>
    </div>
    <div id="goodsList" class="goods-info">
        <!--<img src="images/goodsimg.png" alt="" class="goods-img">-->
        <!--<div class="center-info">-->
            <!--<span class="goods-name">邦伲德VIP一年尊享</span>-->
            <!--<span>优惠价格</span>-->
            <!--<span style="color:#f00;" class="goods-price">￥10000.00</span>-->
            <!--<span class="num">×1</span>-->
        <!--</div>-->
    </div>
    <div class="payment-method">
        <div class="payment-method-title">支付方式</div>
        <div class="payment-method-list">
            <ul id="payCode">
                <!--<li style="background: #fff url(images/confirm-zfb.png) no-repeat 4% center;background-size:0.22rem 0.22rem;">支付宝-->
                    <!--<div class="choice-btn" id="aliPay" onclick="xz(this)"></div>-->
                <!--</li>-->
                <li style="background: #fff url(images/confirm-wx.png) no-repeat 4% center;background-size:0.21rem 0.17rem;">微信支付
                    <div class="choice-btn active" id="wxPay" onclick="xz(this)"></div>
                </li>
                <li style="background: #fff url(images/confirm-ye.png) no-repeat 4% center;background-size:0.22rem 0.22rem;">余额支付
                    <div class="choice-btn" id="code" onclick="xz(this)"></div>
                </li>
            </ul>
        </div>
    </div>
    <div class="pay-detail">
        <ul>
            <li>结算详情：</li>
            <!--<span class="right-text or-coupon">有优惠券？</span>-->
            <li>商品总价：<span id="goods_num" class="right-text"></span></li>
        </ul>
        <ul>
            <!-- <li>运费: -->
                <!--<span class="right-text">￥0.00</span>-->
                <!-- <span class="right-text derate">包邮</span> -->
            <!-- </li> -->
            <a href="confirmOrder_coupon.html">
                <li class="coupon" style="border:none;">优惠券：<span id="reduction"> 0 </span>
                    <span class="right-text derate">减免</span>
                </li>
            </a>
        </ul>
        <ul>
            <li>留言：<input id="userNote" type="text" placeholder="给我们留言"></li>
            <li class="mm-box">支付密码：<input id="password" type="password" placeholder="请输入支付密码" class="mm"></li>
            <li class="all-price">应付：
                <span class="price">￥</span>
                <span id="submitPrice" class="price">
                    <!--提交价格-->
                </span>
                <div class="sub-order" onclick="payment()">提交订单</div>
            </li>
        </ul>
    </div>
</div>
<input type="hidden" id="cid" name="cid" value="商品类型ID">
</body>
<script>
$('.public-left').click(function(){
    window.history.go(-1);
});
function xz(obj){
    $('.choice-btn').removeClass('active');
    $(obj).addClass('active').closest("li").siblings().find(".choice-btn").removeClass('active');
    if($(obj).addClass('active').closest("li").index() == 2){
        $('.mm-box').css('display','block');
    }else{
        $('.mm-box').css('display','none');
    }
    //判断支付的方式！！！！！！
    var pay_code = $('#payCode .active')[0].id; //支付方式
    if (pay_code == 'aliPay') {
        alert ('支付宝功能暂未开放！');
    } if (pay_code == 'code') {
        var user_id = sessionStorage.getItem('userId');
        var submitPrice = $("#submitPrice").html();
        $.ajax({
            url: url() + 'api/users/userIsPwd', //请求的url地址
            dataType: "JSON",   //返回格式为json
            async: true,//请求是否异步，默认为异步，这也是ajax重要特性
            type: "POST",   //请求方式
            data:{
                user_id:user_id,
                submitPrice:submitPrice,
            },
            success: function (data) {
                // console.log(data);
                //请求成功时处理
                if (data.status == 1) {
                    $('.mm-box').css('display', 'block');
                } else {
                    $('.mm-box').css('display', 'none');
                    alert(data.msg);
                }
            }
        });
    }
}
</script>
<script>
    $(function(){
        //验证是否登陆
        // proving();
        var user_id = sessionStorage.getItem('userId');
        if (user_id == null) {
            alert("请您先登录！");
            window.location.href = 'login.html';
        }
        //立即购买
        immediately();
    });
    //立即购买
    function immediately() {
        var user_id = sessionStorage.getItem("userId"); //用户ID
        var goods_id = sessionStorage.getItem("goods_id"); //商品ID，sesion储存，浏览器关闭（清空）
        var item_id = sessionStorage.getItem("item_id"); //规格ID，sesion储存，浏览器关闭（清空）
        var goods_num = sessionStorage.getItem("goods_num"); //商品数量，sesion储存，浏览器关闭（清空）

        $.ajax({
            url: url() + 'api/order/confirm', //请求的url地址
            dataType: "JSON",   //返回格式为json
            async: true,//请求是否异步，默认为异步，这也是ajax重要特性
            type: "POST",   //请求方式
            data:{
                user_id:user_id,
                goods_id:goods_id,
                item_id:item_id,
                goods_num:goods_num,
            },
            success: function (data) {
                //请求成功时处理
                if(data.status==1){
                   // console.log(data.result);
                   var html = '';
                    $.each(data.result, function (i, item) {
                        var img = imgUrl() + item.goods.original_img;
                        html += `
                            <a href="productDetail.html?goods_id=${item.goods_id}">
                            <img src="${img}" alt="" class="goods-img">
                            <div class="center-info">
                                <span class="goods-name">${item.goods_name}${item.spec_key_name}尊享</span>
                                <span>优惠价格</span>
                                <span style="color:#f00;" class="goods-price">￥${item.total_fee}</span>
                                <span class="num">×${item.goods_num}</span>
                            </div>
                            </a>
                            `;
                        $('#goods_num').html('￥'+item.total_fee);
                        //判断是否使用优惠券(如果使用修改最终提交价格)
                        var favorablePrice = sessionStorage.getItem('favorablePrice');
                        $('#reduction').html(favorablePrice);
                        $('#submitPrice').html(item.total_fee-favorablePrice);
                        sessionStorage.setItem('cid',item.goods.cat_id); //商品类型，sesion储存，浏览器关闭（清空）
                    });
                   $('#goodsList').html(html);
                } else {
                    alert(data.msg);
                }
            }
        });
    }

    //提交订单
    function payment() {
        var user_id = sessionStorage.getItem('userId');
        var goods_id = sessionStorage.getItem('goods_id');
        var goods_num = sessionStorage.getItem('goods_num');
        var item_id = sessionStorage.getItem('item_id');
        var user_note = $('#userNote').val(); //留言
        //判断是否使用优惠券！！！！！！
        var key = JSON.parse(sessionStorage.getItem("favorableId"));
        if(key==""||key==null){
            var coupon_id = '';
        } else {
            var coupon_id = sessionStorage.getItem('favorableId');
        }
        //判断支付的方式！！！！！！
        var pay_code = $('#payCode .active')[0].id; //支付方式
        if (pay_code == 'aliPay') {
            alert ('支付宝功能暂未开放,请选择其他支付方式！');
            return false;
        }
        if (pay_code == 'code') {
            var payPwd = $('#password').val();
            if (payPwd == '') {
                alert('请您输入-支付密码！');
                return false;
            }
        } else {
            payPwd = '';
        }
        //清空购买的信息
        sessionStorage.removeItem('favorablePrice');
        sessionStorage.removeItem('favorableId');
        sessionStorage.removeItem('cid');
        $.ajax({
            url: url() + 'api/order/placeOrder', //请求的url地址
            dataType: "JSON",   //返回格式为json
            async: true,//请求是否异步，默认为异步，这也是ajax重要特性
            type: "POST",   //请求方式
            data:{
                user_id:user_id,
                goods_id:goods_id,
                item_id:item_id,
                goods_num:goods_num,
                user_note:user_note,
                coupon_id:coupon_id,
                pay_code:pay_code,
                payPwd:payPwd,
                //address_id:address_id,
            },
            success: function (data) {
                console.log(data);
                //请求成功时处理
                if(data.status==1){
                    var out_trade_no = data.result.order_sn;
                    var total_fee = data.result.order_calculate.order_amount;
                    // console.log(data.result.order_calculate.order_amount);
                    //判断支付方式
                    if (pay_code == 'aliPay') {
                        alert ('支付宝功能暂未开放！');
                    } else if (pay_code == 'wxPay') {
                        window.location.href=`http://vip.bndvip.com/api/Pay/wxPay?out_trade_no=${out_trade_no}&total_fee=${total_fee}`;
                    } else {
                       window.location.href=`http://vip.bndvip.com/api/Pay/balancePay?out_trade_no2=${out_trade_no}`;
                    }
                } else {
                    alert(data.msg);
                }
            }
        });
    }

</script>

</html>






